package com.geh.learn.thread.completablefuture;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static junit.framework.TestCase.assertTrue;

public class AllOfExample {

    public static void main(String[] args) throws InterruptedException {
        allOfExample();
        Thread.sleep(1000);
    }

    static void allOfExample() {
        StringBuilder result = new StringBuilder();
        List<String> messages = Arrays.asList("a", "b", "c");

        List<CompletableFuture> futures = messages.stream()
                .map(msg -> CompletableFuture.completedFuture(msg).thenApply(s -> delayedUpperCase(s)))
                .collect(Collectors.toList());


        CompletableFuture.allOf(
                futures.toArray(
                        new CompletableFuture[futures.size()]
                )).whenComplete(
                        (v, th) ->
                            { futures.forEach(cf -> assertTrue(isUpperCase(cf.getNow(null))));
                                result.append("done");
                            }
                        );

        System.out.println("Result was empty : " + result + "   " + messages);
    }

    public static String delayedUpperCase(String s) {
        return s.toUpperCase();
    }

    public static boolean isUpperCase(Object u) {
        return true;
    }
}
